//
//  GetPushParaViewController.m
//  YMDemo
//
//  Created by admin on 2022/4/13.
//

#import "GetPushParaViewController.h"
#import "NetworkUtils.h"
#import <Masonry/Masonry.h>
#import "UIButton+TQEasyIcon.h"
#import "UIButton+Gradient.h"
#import "UIViewController+HUD.h"
#import "MainViewController.h"
@interface GetPushParaViewController ()<UITextFieldDelegate,UITextViewDelegate,UIScrollViewDelegate>
@property (nonatomic,strong)UILabel *uidLable;
@property (nonatomic,strong)UITextField *uidText;
@property (nonatomic,strong)UIButton *uidButton;

@property (nonatomic,strong)UILabel *urlLable;
@property (nonatomic,strong)UILabel *urlText;
@property (nonatomic,strong)UIButton *urlButton;

@property (nonatomic,strong)UILabel *codeLable;
@property (nonatomic,strong)UILabel *codeText;
@property (nonatomic,strong)UIButton *codeButton;

@property (nonatomic,strong)UILabel *bindLable;
@property (nonatomic,strong)UITextView *bindTextView;
@property (nonatomic,strong)UIButton *bindButton;
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)NSString *token;
@end

@implementation GetPushParaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"创建直播";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initView];
   
    // Do any additional setup after loading the view.
}
-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:@"token"];
    [userDefaults synchronize];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.uidText resignFirstResponder];
    [self.bindTextView resignFirstResponder];
}
-(void)tapResignFirstResponder{
    
    [self.uidText resignFirstResponder];
    [self.bindTextView resignFirstResponder];
}
#pragma mark
-(void)initView{
    
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(0, 1200);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapResignFirstResponder)];
    [self.scrollView addGestureRecognizer:tap];
    [self.scrollView addSubview:self.uidLable];
    [self.uidLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.scrollView.mas_left).offset(15);
        make.top.mas_equalTo(self.scrollView.mas_top).offset(25);
        
    }];
    [self.scrollView addSubview:self.uidText];
    [self.uidText mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidLable.mas_left);
        make.top.mas_equalTo(self.uidLable.mas_bottom).offset(15);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width-30);
        make.height.mas_equalTo(40);
        
    }];
    [self.scrollView addSubview:self.uidButton];
    [self.uidButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.scrollView.mas_centerX);
        make.top.mas_equalTo(self.uidText.mas_bottom).offset(25);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40));

    }];
    
    [self.uidButton gradientButtonWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40) colorArray:@[(id)[UIColor colorWithRed:84/255.0 green:177/255.0 blue:246/255.0 alpha:1],(id)[UIColor colorWithRed:58/255.0 green:127/255.0 blue:248/255.0 alpha:1]] percentageArray:@[@(0.18),@(1)] gradientType:GradientFromLeftToRight];
    
    [self.scrollView addSubview:self.urlLable];
    [self.urlLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidLable.mas_left);
        make.top.mas_equalTo(self.uidButton.mas_bottom).offset(40);
        
    }];
    
    [self.scrollView addSubview:self.urlText];
    [self.urlText mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidText.mas_left);
        make.top.mas_equalTo(self.urlLable.mas_bottom).offset(15);
        make.width.mas_equalTo(self.uidText.mas_width);
        make.height.mas_equalTo(self.uidText.mas_height);
        
    }];
    [self.scrollView addSubview:self.urlButton];
    [self.urlButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.scrollView.mas_centerX);
        make.top.mas_equalTo(self.urlText.mas_bottom).offset(25);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40));

    }];
    
    [self.urlButton gradientButtonWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40) colorArray:@[(id)[UIColor colorWithRed:84/255.0 green:177/255.0 blue:246/255.0 alpha:1],(id)[UIColor colorWithRed:58/255.0 green:127/255.0 blue:248/255.0 alpha:1]] percentageArray:@[@(0.18),@(1)] gradientType:GradientFromLeftToRight];
    
    [self.scrollView addSubview:self.codeLable];
    [self.codeLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidLable.mas_left);
        make.top.mas_equalTo(self.urlButton.mas_bottom).offset(40);
        
    }];
    
    [self.scrollView addSubview:self.codeText];
    [self.codeText mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidText.mas_left);
        make.top.mas_equalTo(self.codeLable.mas_bottom).offset(15);
        make.width.mas_equalTo(self.uidText.mas_width);
        make.height.mas_equalTo(self.uidText.mas_height);
        
    }];
    [self.scrollView addSubview:self.codeButton];
    [self.codeButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.scrollView.mas_centerX);
        make.top.mas_equalTo(self.codeText.mas_bottom).offset(25);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40));

    }];
    
    [self.codeButton gradientButtonWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40) colorArray:@[(id)[UIColor colorWithRed:84/255.0 green:177/255.0 blue:246/255.0 alpha:1],(id)[UIColor colorWithRed:58/255.0 green:127/255.0 blue:248/255.0 alpha:1]] percentageArray:@[@(0.18),@(1)] gradientType:GradientFromLeftToRight];
    
    [self.scrollView addSubview:self.bindLable];
    [self.bindLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidLable.mas_left);
        make.top.mas_equalTo(self.codeButton.mas_bottom).offset(40);
        
    }];
    
    [self.scrollView addSubview:self.bindTextView];
    [self.bindTextView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidText.mas_left);
        make.top.mas_equalTo(self.bindLable.mas_bottom).offset(15);
        make.width.mas_equalTo(self.uidText.mas_width);
        make.height.mas_equalTo(90);
        
    }];
    [self.scrollView addSubview:self.bindButton];
    [self.bindButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.scrollView.mas_centerX);
        make.top.mas_equalTo(self.bindTextView.mas_bottom).offset(25);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40));

    }];
    
    [self.bindButton gradientButtonWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40) colorArray:@[(id)[UIColor colorWithRed:84/255.0 green:177/255.0 blue:246/255.0 alpha:1],(id)[UIColor colorWithRed:58/255.0 green:127/255.0 blue:248/255.0 alpha:1]] percentageArray:@[@(0.18),@(1)] gradientType:GradientFromLeftToRight];
   
   
    
}
#pragma mark 用户登录
-(void)loginAction{
    
    if ([self.uidText.text isEqualToString:@""]||self.uidText.text == nil||[self.uidText.text isEqualToString:@"(null)"]) {
        
        [self showHint:@"用户Id不能为空"];
        return;
    }
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"uid"] = self.uidText.text;
    params[@"username"] = @"";
    params[@"appKey"] = kYMAppKey;
    params[@"appSecret"] = kYMAppSecret;
    params[@"type"] = @"3";
    [NetworkUtils postjson:@"https://im.yimaxiaoerlang.cn/chat/login" parameters:params succes:^(NSDictionary * _Nonnull dic) {
        
        NSString *token = [NSString stringWithFormat:@"%@",[[dic valueForKey:@"data"] valueForKey:@"token"]];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setValue:token forKey:@"token"];
        [userDefaults synchronize];
        [self showHint:@"登录成功"];
        self.urlText.text = @"";
        self.codeText.text = @"";
        self.bindTextView.text = @"";
        
        
    } failure:^(NSError * _Nonnull error) {
        
        [self showHint:@"登录失败"];
    }];
}
#pragma mark 获取推流链接
-(void)urlAction{

    [NetworkUtils getjson:@"https://im.yimaxiaoerlang.cn/live/anchor/push/url" parameters:@{} succes:^(NSDictionary * _Nonnull dic) {
        
        self.urlText.text= [NSString stringWithFormat:@"%@",[dic valueForKey:@"data"]];
        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
        paraStyle.alignment = NSTextAlignmentLeft;  //对齐
        paraStyle.headIndent = 0.0f;//行首缩进
        //参数：（字体大小17号字乘以2，34f即首行空出两个字符）
        CGFloat emptylen = self.urlText.font.pointSize * 1;
        paraStyle.firstLineHeadIndent = emptylen;//首行缩paraStyle
        paraStyle.tailIndent = 0.0f;//行尾缩进
        paraStyle.lineSpacing = 2.0f;//行间距
        NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:self.urlText.text attributes:@{NSParagraphStyleAttributeName:paraStyle}];
        self.urlText.attributedText = attrText;
        [self showHint:@"获取推流链接成功"];
        
    } failure:^(NSError * _Nonnull error) {
        
        [self showHint:@"获取推流链接失败"];
    }];
        
}
#pragma mark 复制推流链接
-(void)tapUrlPaste{
    
    if ([self.urlText.text isEqualToString:@""]||self.urlText.text == nil) {
        
        [self showHint:@"不能复制空的推流链接"];
        
    }else{
        
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        board.string = self.urlText.text;
        [self showHint:@"推流链接复制成功"];
    }
    
}
#pragma mark 获取推流码
-(void)codeAction{
    
    [NetworkUtils getjson:@"https://im.yimaxiaoerlang.cn/live/anchor/push/code" parameters:@{} succes:^(NSDictionary * _Nonnull dic) {
        
        NSString *codeOrigin = [NSString stringWithFormat:@"%@",[dic valueForKey:@"data"]];
        self.codeText.text = [NSString stringWithFormat:@"%@",[dic valueForKey:@"data"]];
        NSString *codeStr = [self.codeText.text substringFromIndex:6];
        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
        paraStyle.alignment = NSTextAlignmentLeft;  //对齐
        paraStyle.headIndent = 0.0f;//行首缩进
        //参数：（字体大小17号字乘以2，34f即首行空出两个字符）
        CGFloat emptylen = self.codeText.font.pointSize * 1;
        paraStyle.firstLineHeadIndent = emptylen;//首行缩paraStyle
        paraStyle.tailIndent = 0.0f;//行尾缩进
        paraStyle.lineSpacing = 2.0f;//行间距
        NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:codeStr attributes:@{NSParagraphStyleAttributeName:paraStyle}];
      
        self.codeText.attributedText = attrText;
        if (self.urlText.text == nil||[self.urlText.text isEqualToString:@""]||[self.urlText.text isEqualToString:@"(null)"]) {
            
            [self showHint:@"获取推流码成功"];
            
        }else{
            
            [self showHint:@"拼接推流链接和推流码成功"];
            self.bindTextView.text = [NSString stringWithFormat:@"%@%@",self.urlText.text,codeOrigin];
        }
    
       
        
        
        
    } failure:^(NSError * _Nonnull error) {
        
        [self showHint:@"获取推流码失败"];
    }];
        
}
#pragma mark 复制推流码
-(void)tapCodePaste{
    
    if ([self.codeText.text isEqualToString:@""]||self.codeText.text == nil) {
        
        [self showHint:@"不能复制空的推流码"];
        
    }else{
        
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        board.string = self.codeText.text;
        [self showHint:@"推流码复制成功"];
    }
}
#pragma mark 创建直播
-(void)bindAction{
    
    if (self.urlText.text == nil||[self.urlText.text isEqualToString:@""]||[self.urlText.text isEqualToString:@"(null)"]) {
        
        [self showHint:@"推流链接错误，暂不能开启直播"];
        
        return;
    }
    if (self.codeText.text == nil||[self.codeText.text isEqualToString:@""]||[self.codeText.text isEqualToString:@"(null)"]) {
        
        [self showHint:@"推流码错误，暂不能开启直播"];
        
        return;
    }
        
    MainViewController *vc = [[MainViewController alloc]init];
    vc.liveUrl = self.bindTextView.text;
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}
#pragma mark 复制直播链接
-(void)tapLivePaste{
    
    if ([self.bindTextView.text isEqualToString:@""]||self.bindTextView.text == nil) {
        
        [self showHint:@"不能复制空的链接"];
        
    }else{
        
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        board.string = self.bindTextView.text;
        [self showHint:@"复制成功"];
    }
}
-(UIScrollView *)scrollView{
    
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.delegate = self;
        _scrollView.showsVerticalScrollIndicator = NO;
        
    }
    return _scrollView;
}
-(UILabel *)uidLable{
    
    if (_uidLable == nil) {
        
        _uidLable = [[UILabel alloc]init];
        _uidLable.text = @"1.请输入用户Id";
        _uidLable.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        _uidLable.font = [UIFont systemFontOfSize:16];
    }
    return _uidLable;
}
-(UITextField *)uidText{
    
    if (_uidText == nil) {
        
        _uidText = [[UITextField alloc]init];
        _uidText.placeholder = @"请输入用户Id";
        _uidText.font = [UIFont systemFontOfSize:16];
        _uidText.textColor = [UIColor blackColor];
        _uidText.delegate = self;
        _uidText.textAlignment = NSTextAlignmentCenter;
        _uidText.layer.borderColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:0.5].CGColor;
        _uidText.layer.borderWidth = 1;
        _uidText.layer.cornerRadius = 21;
        _uidText.clipsToBounds = YES;
        _uidText.keyboardType = UIKeyboardTypeNumberPad;
    }
    
    return _uidText;
}

-(UIButton *)uidButton{
    
    if (_uidButton == nil) {
        
        _uidButton = [[UIButton alloc]init];
        [_uidButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
        [_uidButton setTitle:@"1.用户登录" forState:UIControlStateNormal];
        _uidButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_uidButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _uidButton.layer.cornerRadius = 18;
        _uidButton.clipsToBounds = YES;
    }
    
    return _uidButton;
}

-(UILabel *)urlLable{
    
    if (_urlLable == nil) {
        
        _urlLable = [[UILabel alloc]init];
        _urlLable.text = @"2.点击获取推流链接,点击复制";
        _urlLable.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        _urlLable.font = [UIFont systemFontOfSize:16];
    }
    return _urlLable;
}
-(UILabel *)urlText{
    
    if (_urlText == nil) {
        
        _urlText = [[UILabel alloc]init];
        _urlText.font = [UIFont systemFontOfSize:16];
        _urlText.textColor = [UIColor blackColor];
        _urlText.textAlignment = NSTextAlignmentLeft;
        _urlText.layer.borderColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:0.5].CGColor;
        _urlText.layer.borderWidth = 1;
        _urlText.layer.cornerRadius = 21;
        _urlText.clipsToBounds = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapUrlPaste)];
        _urlText.userInteractionEnabled = YES;
        [_urlText addGestureRecognizer:tap];
    }
    
    return _urlText;
}

-(UIButton *)urlButton{
    
    if (_urlButton == nil) {
        
        _urlButton = [[UIButton alloc]init];
        [_urlButton addTarget:self action:@selector(urlAction) forControlEvents:UIControlEventTouchUpInside];
        [_urlButton setTitle:@"2.获取推流链接" forState:UIControlStateNormal];
        _urlButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_urlButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _urlButton.layer.cornerRadius = 18;
        _urlButton.clipsToBounds = YES;
    }
    
    return _urlButton;
}
-(UILabel *)codeLable{
    
    if (_codeLable == nil) {
        
        _codeLable = [[UILabel alloc]init];
        _codeLable.text = @"3.点击获取推流码,点击复制";
        _codeLable.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        _codeLable.font = [UIFont systemFontOfSize:16];
      
    }
    return _codeLable;
}
-(UILabel *)codeText{
    
    if (_codeText == nil) {
        
        _codeText = [[UILabel alloc]init];
        _codeText.font = [UIFont systemFontOfSize:16];
        _codeText.textColor = [UIColor blackColor];
        _codeText.textAlignment = NSTextAlignmentLeft;
        _codeText.layer.borderColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:0.5].CGColor;
        _codeText.layer.borderWidth = 1;
        _codeText.layer.cornerRadius = 21;
        _codeText.clipsToBounds = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCodePaste)];
        _codeText.userInteractionEnabled = YES;
        [_codeText addGestureRecognizer:tap];
    }
    
    return _codeText;
}

-(UIButton *)codeButton{
    
    if (_codeButton == nil) {
        
        _codeButton = [[UIButton alloc]init];
        [_codeButton addTarget:self action:@selector(codeAction) forControlEvents:UIControlEventTouchUpInside];
        [_codeButton setTitle:@"3.获取推流码" forState:UIControlStateNormal];
        _codeButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _codeButton.layer.cornerRadius = 18;
        _codeButton.clipsToBounds = YES;
    }
    
    return _codeButton;
}
-(UILabel *)bindLable{
    
    if (_bindLable == nil) {
        
        _bindLable = [[UILabel alloc]init];
        _bindLable.text = @"4.拼接推流链接和推流码,点击创建直播,开始直播";
        _bindLable.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        _bindLable.font = [UIFont systemFontOfSize:16];
        
    }
    return _bindLable;
}
-(UITextView *)bindTextView{
    
    if (_bindTextView == nil) {
        
        _bindTextView = [[UITextView alloc]init];
        _bindTextView.font = [UIFont systemFontOfSize:16];
        _bindTextView.textColor = [UIColor blackColor];
        _bindTextView.layer.borderColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:0.5].CGColor;
        _bindTextView.layer.borderWidth = 1;
        _bindTextView.layer.cornerRadius = 10;
        _bindTextView.clipsToBounds = YES;
        _bindTextView.userInteractionEnabled = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapLivePaste)];
        [_bindTextView addGestureRecognizer:tap];
    }
    
    return _bindTextView;
}

-(UIButton *)bindButton{
    
    if (_bindButton == nil) {
        
        _bindButton = [[UIButton alloc]init];
        [_bindButton addTarget:self action:@selector(bindAction) forControlEvents:UIControlEventTouchUpInside];
        [_bindButton setTitle:@"4.创建直播" forState:UIControlStateNormal];
        _bindButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_bindButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _bindButton.layer.cornerRadius = 18;
        _bindButton.clipsToBounds = YES;
    }
    
    return _bindButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
